﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Openlab.Tools.CodeGender
{
    public class StoredProcedureParameter
    {
        public StoredProcedureParameter(Column column)
        {
            this.Name = column.Name;
            this.DbType = column.DBTypeName;
            this.Size = column.Length;
        }

        public StoredProcedureParameter(string name, string type, int size)
        {
            this.Name = name;
            this.DbType = type;
            this.Size = size;
            this.ParameterDirection = ParameterDirection.Input;
        }

        public string Name { get; set; }

        public string DbType { get; set; }

        public int Size { get; set; }

        public ParameterDirection ParameterDirection { get; set; }

        public string Sql
        {
            get {
                StringBuilder sql = new StringBuilder();
                string dir = string.Empty;
                if (ParameterDirection == ParameterDirection.Output)
                    dir = "\tOUT";
                string sizeText = string.Empty;
                if (DbType.IndexOf("char", StringComparison.InvariantCultureIgnoreCase) > -1)
                    sizeText = string.Format("({0})", Size);
                sql.AppendFormat("@{0}  {1}{2}{3}", Name, DbType, sizeText, dir);
                return sql.ToString();
            }
        }
    }
}
